﻿namespace Chain.Common
{
    using System;
    using System.Configuration;
    using System.IO;
    using System.Reflection;
    using System.Web;
    using System.Xml;
    using System.Xml.XPath;

    public class ConfigHelper
    {
        private static int _configType = 0;
        private static string docName = string.Empty;
        private static XmlNode node = null;

        public static string GetValue(string key)
        {
            string text1 = "AppSettings-" + key;
            object obj2 = new object();
            try
            {
                obj2 = ConfigurationManager.AppSettings[key];
            }
            catch
            {
            }
            return obj2.ToString();
        }

        /// <summary>
        /// app_data文件夹 xml读取数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValueFromData(string key)
        {
            string value = "";
            try
            {               
                System.Xml.XmlDocument xx = new System.Xml.XmlDocument();
                string filePath = HttpContext.Current.Request.MapPath("~/App_Data/SystemDomain.xml");
                if (File.Exists(filePath))
                {
                    xx.Load(filePath);
                    System.Xml.XmlNode item = xx.SelectSingleNode("xml");
                    System.Xml.XmlNode item2 = xx.SelectSingleNode("System");

                    value = item2.SelectSingleNode("Domain").InnerText.Trim();
                }
            }
            catch { }
            return value;
        }

        private static XmlDocument loadConfigDoc(XmlDocument cfgDoc)
        {
            if (Convert.ToInt32(ConfigType) == Convert.ToInt32(ConfigFileType.AppConfig))
            {
                docName = Assembly.GetEntryAssembly().GetName().Name;
                docName = docName + ".exe.config";
            }
            else
            {
                string str = (HttpContext.Current.Request.ApplicationPath != "/") ? HttpContext.Current.Request.ApplicationPath : "";
                docName = HttpContext.Current.Server.MapPath(str + "/web.config");
            }
            cfgDoc.Load(docName);
            return cfgDoc;
        }

        public static bool modifyElement(string elementKey)
        {
            try
            {
                XmlDocument cfgDoc = new XmlDocument();
                loadConfigDoc(cfgDoc);
                node = cfgDoc.SelectSingleNode("//appSettings");
                if (node == null)
                {
                    throw new InvalidOperationException("appSettings section not found");
                }
                node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
                saveConfigDoc(cfgDoc, docName);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool removeElement(string elementKey)
        {
            try
            {
                XmlDocument cfgDoc = new XmlDocument();
                loadConfigDoc(cfgDoc);
                node = cfgDoc.SelectSingleNode("//appSettings");
                if (node == null)
                {
                    throw new InvalidOperationException("appSettings section not found");
                }
                node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
                saveConfigDoc(cfgDoc, docName);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private static void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath)
        {
            try
            {
                XmlTextWriter w = new XmlTextWriter(cfgDocPath, null);
                w.Formatting = Formatting.Indented;
                cfgDoc.WriteTo(w);
                w.Flush();
                w.Close();
            }
            catch
            {
                throw;
            }
        }

        public static bool SetValue(string key, string value)
        {
            XmlDocument cfgDoc = new XmlDocument();
            loadConfigDoc(cfgDoc);
            node = cfgDoc.SelectSingleNode("//appSettings");
            if (node == null)
            {
                throw new InvalidOperationException("appSettings section not found");
            }
            try
            {
                XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
                if (element != null)
                {
                    element.SetAttribute("value", value);
                }
                else
                {
                    XmlElement newChild = cfgDoc.CreateElement("add");
                    newChild.SetAttribute("key", key);
                    newChild.SetAttribute("value", value);
                    node.AppendChild(newChild);
                }
                saveConfigDoc(cfgDoc, docName);
                string text1 = "AppSettings-" + key;
                return true;
            }
            catch
            {
                return false;
            }
        }

        private static int ConfigType
        {
            get
            {
                return _configType;
            }
            set
            {
                _configType = value;
            }
        }
    }
}

